package com.example.loader;

import com.hazelcast.cluster.MembershipEvent;
import com.hazelcast.cluster.MembershipListener;
import com.hazelcast.core.HazelcastInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/*
 * @className: HazelcastMembershipLoader
 * @author: chentao
 * @date: 2025/2/19 上午8:24
 * @Version: 1.0
 * @description:
 */
@Service
public class HazelcastMembershipLoader implements HazelcastLoader {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void init(HazelcastInstance hazelcastInstance) throws Exception {
        hazelcastInstance.getCluster().addMembershipListener(new MembershipListener() {
            @Override
            public void memberAdded(MembershipEvent membershipEvent) {
                logger.warn("节点加入集群: " + membershipEvent.getMember().getAddress());
            }

            @Override
            public void memberRemoved(MembershipEvent membershipEvent) {
                logger.warn("节点离开集群: " + membershipEvent.getMember().getAddress());
            }
        });
    }
}
